// pages/my/myFactory/editMyFactory.js
const app = getApp()
Page({
  data: {
    region: ['请选择地区'],
    color:false,
    id:'',
    name:'',
    phone:'',
    address:'',
    type:'',
    agentId:''
    // customItem: '全部'
  },
  onLoad: function (options) {
    let type = options.type //0编辑1添加
    if (type == 0){  
      let address = JSON.parse(options.address)
      wx.setNavigationBarTitle({
        title: '编辑地址' 
      })
      this.setData({
        type,
        id:address.id,
        name:address.contactName,
        phone:address.mobilePhone,
        address:address.address,
        region:[address.provinceName,address.cityName,address.countryName]
      })
    }else{
      wx.setNavigationBarTitle({
        title: '添加新地址' 
      })
      this.setData({
        type
      })
    }
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
    })
  },
  onShow: function () {

  },
  bindRegionChange(e) {
    console.log(e.detail.value)
    this.setData({
      region: e.detail.value,
      color:true
    })
  },
  getName(e){
    this.setData({
      name:e.detail.value
    })
  },
  getPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  getAddress(e){
    this.setData({
      address:e.detail.value
    })
  },
  save(){
    if (this.data.type==0) {
      if (this.data.name=='') {
        app.errorMsg('请填写姓名')
      }else if (this.data.phone=='') {
        app.errorMsg('请填写手机号')
      }else if (this.data.phone.toString().length!=11) {
        app.errorMsg('请输入正确的手机号')
      }else if (this.data.region.length<2) {
        app.errorMsg('请选择地区')
      }else if (this.data.address=='') {
        app.errorMsg('请填写详细地址')
      }else{    
        let data = {
          id:this.data.id,
          contactName:this.data.name,
          mobilePhone:this.data.phone,
          provinceName:this.data.region[0],
          cityName:this.data.region[1],
          countryName:this.data.region[2],
          address:this.data.address
        }
        console.log(data)
        app.request('wx_app/agent/address/save',data,'GET').then(res=>{
          console.log(res)
          if (res.code==1) {
            wx.showToast({
              title:'编辑成功'
            })
            setTimeout(function(){
              wx.navigateBack()
            },1000)
          }else{
            app.errorMsg(res.message)
          }
        })
      }
    }else{
      if (this.data.name=='') {
        app.errorMsg('请填写姓名')
      }else if (this.data.phone=='') {
        app.errorMsg('请填写手机号')
      }else if (this.data.phone.toString().length!=11) {
        app.errorMsg('请输入正确的手机号')
      }else if (this.data.region.length<2) {
        app.errorMsg('请选择地区')
      }else if (this.data.address=='') {
        app.errorMsg('请填写详细地址')
      }else{    
        let data = {
          agentId:this.data.agentId,
          contactName:this.data.name,
          mobilePhone:this.data.phone,
          provinceName:this.data.region[0],
          cityName:this.data.region[1],
          countryName:this.data.region[2],
          address:this.data.address
        }
        console.log(data)
        app.request('wx_app/agent/address/save',data,'GET').then(res=>{
          console.log(res)
          if (res.code==1) {
            wx.showToast({
              title:'添加成功'
            })
            setTimeout(function(){
              wx.navigateBack()
            },1000)
          }else{
            app.errorMsg(res.message)
          }
        })
      }
    }
  },
  
  del(){
    let that = this
    wx.showModal({
      title:'',
      content:'确认要删除此收货地址吗？',
      confirmText:'删除',
      confirmColor:'#0194EC',
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          let data = {
            id:that.data.id
          }
          app.request('wx_app/agent/address/del',data,'GET').then(res=>{
            console.log(res)
            if(res.code==1){
              wx.showToast({
                title:'删除成功'
              })
              setTimeout(function(){
                wx.navigateBack()
              },1000)
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      },
      fail(){
        app.errorMsg(res.message)
      }
    })

  }
})
















